home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Berkeley DB 1.8.5a / hash / hash_buf.c < prev    next >
Text File  |  1995-06-22  |  10KB  |  373 lines

  1. /*-
  2.  * Copyright (c) 1990, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Margo Seltzer.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)hash_buf.c    8.5 (Berkeley) 7/15/94";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. /*
  42.  * PACKAGE: hash
  43.  *
  44.  * DESCRIPTION:
  45.  *    Contains buffer management
  46.  *
  47.  * ROUTINES:
  48.  * External
  49.  *    __buf_init
  50.  *    __get_buf
  51.  *    __buf_free
  52.  *    __reclaim_buf
  53.  * Internal
  54.  *    newbuf
  55.  */
  56.  
  57. #include <sys/param.h>
  58.  
  59. #include <errno.h>
  60. #include <stddef.h>
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63.  
  64. #ifdef DEBUG
  65. #include <assert.h>
  66. #endif
  67.  
  68. #include <db.h>
  69. #include "hash.h"
  70. #include "page.h"
  71. #include "hash_extern.h"
  72.  
  73. static BUFHEAD *newbuf __P((HTAB *, u_int32_t, BUFHEAD *));
  74.  
  75. /* Unlink B from its place in the lru */
  76. #define BUF_REMOVE(B) { \
  77.     (B)->prev->next = (B)->next; \
  78.     (B)->next->prev = (B)->prev; \
  79. }
  80.  
  81. /* Insert B after P */
  82. #define BUF_INSERT(B, P) { \
  83.     (B)->next = (P)->next; \
  84.     (B)->prev = (P); \
  85.     (P)->next = (B); \
  86.     (B)->next->prev = (B); \
  87. }
  88.  
  89. #define    MRU    hashp->bufhead.next
  90. #define    LRU    hashp->bufhead.prev
  91.  
  92. #define MRU_INSERT(B)    BUF_INSERT((B), &hashp->bufhead)
  93. #define LRU_INSERT(B)    BUF_INSERT((B), LRU)
  94.  
  95. /*
  96.  * We are looking for a buffer with address "addr".  If prev_bp is NULL, then
  97.  * address is a bucket index.  If prev_bp is not NULL, then it points to the
  98.  * page previous to an overflow page that we are trying to find.
  99.  *
  100.  * CAVEAT:  The buffer header accessed via prev_bp's ovfl field may no longer
  101.  * be valid.  Therefore, you must always verify that its address matches the
  102.  * address you are seeking.
  103.  */
  104. extern BUFHEAD *
  105. __get_buf(hashp, addr, prev_bp, newpage)
  106.     HTAB *hashp;
  107.     u_int32_t addr;
  108.     BUFHEAD *prev_bp;
  109.     int newpage;    /* If prev_bp set, indicates a new overflow page. */
  110. {
  111.     register BUFHEAD *bp;
  112.     register u_int32_t is_disk_mask;
  113.     register int is_disk, segment_ndx;
  114.     SEGMENT segp;
  115.  
  116.     is_disk = 0;
  117.     is_disk_mask = 0;
  118.     if (prev_bp) {
  119.         bp = prev_bp->ovfl;
  120.         if (!bp || (bp->addr != addr))
  121.             bp = NULL;
  122.         if (!newpage)
  123.             is_disk = BUF_DISK;
  124.     } else {
  125.         /* Grab buffer out of directory */
  126.         segment_ndx = addr & (hashp->SGSIZE - 1);
  127.  
  128.         /* valid segment ensured by __call_hash() */
  129.         segp = hashp->dir[addr >> hashp->SSHIFT];
  130. #ifdef DEBUG
  131.         assert(segp != NULL);
  132. #endif
  133.         bp = PTROF(segp[segment_ndx]);
  134.         is_disk_mask = ISDISK(segp[segment_ndx]);
  135.         is_disk = is_disk_mask || !hashp->new_file;
  136.     }
  137.  
  138.     if (!bp) {
  139.         bp = newbuf(hashp, addr, prev_bp);
  140.         if (!bp ||
  141.             __get_page(hashp, bp->page, addr, !prev_bp, is_disk, 0))
  142.             return (NULL);
  143.         if (!prev_bp)
  144.             segp[segment_ndx] =
  145.                 (BUFHEAD *)((ptrdiff_t)bp | is_disk_mask);
  146.     } else {
  147.         BUF_REMOVE(bp);
  148.         MRU_INSERT(bp);
  149.     }
  150.     return (bp);
  151. }
  152.  
  153. /*
  154.  * We need a buffer for this page. Either allocate one, or evict a resident
  155.  * one (if we have as many buffers as we're allowed) and put this one in.
  156.  *
  157.  * If newbuf finds an error (returning NULL), it also sets errno.
  158.  */
  159.  
  160. #ifdef macintosh
  161. /* The Mac doesn't guarantee long alignment */
  162. void * lalloc(size_t size)
  163. {
  164.     short * buf = (short *) malloc(size+2);
  165.     if ((unsigned long)buf & 2)
  166.         ++buf;
  167.     
  168.     return buf;
  169. }
  170. #endif
  171.  
  172. static BUFHEAD *
  173. newbuf(hashp, addr, prev_bp)
  174.     HTAB *hashp;
  175.     u_int32_t addr;
  176.     BUFHEAD *prev_bp;
  177. {
  178.     register BUFHEAD *bp;        /* The buffer we're going to use */
  179.     register BUFHEAD *xbp;        /* Temp pointer */
  180.     register BUFHEAD *next_xbp;
  181.     SEGMENT segp;
  182.     int segment_ndx;
  183.     u_int16_t oaddr, *shortp;
  184.  
  185.     oaddr = 0;
  186.     bp = LRU;
  187.     /*
  188.      * If LRU buffer is pinned, the buffer pool is too small. We need to
  189.      * allocate more buffers.
  190.      */
  191.     if (hashp->nbufs || (bp->flags & BUF_PIN)) {
  192.         /* Allocate a new one */
  193. #ifdef macintosh
  194.         if ((bp = (BUFHEAD *)lalloc(sizeof(BUFHEAD))) == NULL)
  195. #else
  196.         if ((bp = (BUFHEAD *)malloc(sizeof(BUFHEAD))) == NULL)
  197. #endif
  198.             return (NULL);
  199. #ifdef PURIFY
  200.         memset(bp, 0xff, sizeof(BUFHEAD));
  201. #endif
  202.         if ((bp->page = (char *)malloc(hashp->BSIZE)) == NULL) {
  203.             free(bp);
  204.             return (NULL);
  205.         }
  206. #ifdef PURIFY
  207.         memset(bp->page, 0xff, hashp->BSIZE);
  208. #endif
  209.         if (hashp->nbufs)
  210.             hashp->nbufs--;
  211.     } else {
  212.         /* Kick someone out */
  213.         BUF_REMOVE(bp);
  214.         /*
  215.          * If this is an overflow page with addr 0, it's already been
  216.          * flushed back in an overflow chain and initialized.
  217.          */
  218.         if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) {
  219.             /*
  220.              * Set oaddr before __put_page so that you get it
  221.              * before bytes are swapped.
  222.              */
  223.             shortp = (u_int16_t *)bp->page;
  224.             if (shortp[0])
  225.                 oaddr = shortp[shortp[0] - 1];
  226.             if ((bp->flags & BUF_MOD) && __put_page(hashp, bp->page,
  227.                 bp->addr, (int)IS_BUCKET(bp->flags), 0))
  228.                 return (NULL);
  229.             /*
  230.              * Update the pointer to this page (i.e. invalidate it).
  231.              *
  232.              * If this is a new file (i.e. we created it at open
  233.              * time), make sure that we mark pages which have been
  234.              * written to disk so we retrieve them from disk later,
  235.              * rather than allocating new pages.
  236.              */
  237.             if (IS_BUCKET(bp->flags)) {
  238.                 segment_ndx = bp->addr & (hashp->SGSIZE - 1);
  239.                 segp = hashp->dir[bp->addr >> hashp->SSHIFT];
  240. #ifdef DEBUG
  241.                 assert(segp != NULL);
  242. #endif
  243.  
  244.                 if (hashp->new_file &&
  245.                     ((bp->flags & BUF_MOD) ||
  246.                     ISDISK(segp[segment_ndx])))
  247.                     segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
  248.                 else
  249.                     segp[segment_ndx] = NULL;
  250.             }
  251.             /*
  252.              * Since overflow pages can only be access by means of
  253.              * their bucket, free overflow pages associated with
  254.              * this bucket.
  255.              */
  256.             for (xbp = bp; xbp->ovfl;) {
  257.                 next_xbp = xbp->ovfl;
  258.                 xbp->ovfl = 0;
  259.                 xbp = next_xbp;
  260.  
  261.                 /* Check that ovfl pointer is up date. */
  262.                 if (IS_BUCKET(xbp->flags) ||
  263.                     (oaddr != xbp->addr))
  264.                     break;
  265.  
  266.                 shortp = (u_int16_t *)xbp->page;
  267.                 if (shortp[0])
  268.                     /* set before __put_page */
  269.                     oaddr = shortp[shortp[0] - 1];
  270.                 if ((xbp->flags & BUF_MOD) && __put_page(hashp,
  271.                     xbp->page, xbp->addr, 0, 0))
  272.                     return (NULL);
  273.                 xbp->addr = 0;
  274.                 xbp->flags = 0;
  275.                 BUF_REMOVE(xbp);
  276.                 LRU_INSERT(xbp);
  277.             }
  278.         }
  279.     }
  280.  
  281.     /* Now assign this buffer */
  282.     bp->addr = addr;
  283. #ifdef DEBUG1
  284.     (void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n",
  285.         bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0);
  286. #endif
  287.     bp->ovfl = NULL;
  288.     if (prev_bp) {
  289.         /*
  290.          * If prev_bp is set, this is an overflow page, hook it in to
  291.          * the buffer overflow links.
  292.          */
  293. #ifdef DEBUG1
  294.         (void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n",
  295.             prev_bp->addr, (prev_bp->ovfl ? bp->ovfl->addr : 0),
  296.             (bp ? bp->addr : 0));
  297. #endif
  298.         prev_bp->ovfl = bp;
  299.         bp->flags = 0;
  300.     } else
  301.         bp->flags = BUF_BUCKET;
  302.     MRU_INSERT(bp);
  303.     return (bp);
  304. }
  305.  
  306. extern void
  307. __buf_init(hashp, nbytes)
  308.     HTAB *hashp;
  309.     int nbytes;
  310. {
  311.     BUFHEAD *bfp;
  312.     int npages;
  313.  
  314.     bfp = &(hashp->bufhead);
  315.     npages = (nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
  316.     npages = MAX(npages, MIN_BUFFERS);
  317.  
  318.     hashp->nbufs = npages;
  319.     bfp->next = bfp;
  320.     bfp->prev = bfp;
  321.     /*
  322.      * This space is calloc'd so these are already null.
  323.      *
  324.      * bfp->ovfl = NULL;
  325.      * bfp->flags = 0;
  326.      * bfp->page = NULL;
  327.      * bfp->addr = 0;
  328.      */
  329. }
  330.  
  331. extern int
  332. __buf_free(hashp, do_free, to_disk)
  333.     HTAB *hashp;
  334.     int do_free, to_disk;
  335. {
  336.     BUFHEAD *bp;
  337.  
  338.     /* Need to make sure that buffer manager has been initialized */
  339.     if (!LRU)
  340.         return (0);
  341.     for (bp = LRU; bp != &hashp->bufhead;) {
  342.         /* Check that the buffer is valid */
  343.         if (bp->addr || IS_BUCKET(bp->flags)) {
  344.             if (to_disk && (bp->flags & BUF_MOD) &&
  345.                 __put_page(hashp, bp->page,
  346.                 bp->addr, IS_BUCKET(bp->flags), 0))
  347.                 return (-1);
  348.         }
  349.         /* Check if we are freeing stuff */
  350.         if (do_free) {
  351.             if (bp->page)
  352.                 free(bp->page);
  353.             BUF_REMOVE(bp);
  354.             free(bp);
  355.             bp = LRU;
  356.         } else
  357.             bp = bp->prev;
  358.     }
  359.     return (0);
  360. }
  361.  
  362. extern void
  363. __reclaim_buf(hashp, bp)
  364.     HTAB *hashp;
  365.     BUFHEAD *bp;
  366. {
  367.     bp->ovfl = 0;
  368.     bp->addr = 0;
  369.     bp->flags = 0;
  370.     BUF_REMOVE(bp);
  371.     LRU_INSERT(bp);
  372. }
  373.